package com.truongnha.clienttn.data.dao;

import com.truongnha.clienttn.data.entity.SavedAccountEntity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SavedAccountDAO {
	private final SQLiteDatabase db;

	public SavedAccountDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}

	public long insert(String username, String password) {
		ContentValues inititalValue = new ContentValues();
		inititalValue.put(SavedAccountScheme.USERNAME, username);
		inititalValue.put(SavedAccountScheme.PASSWORD, password);
		return db.insert(SavedAccountScheme.TABLE_NAME, null, inititalValue);
	}

	public void delete() {
		db.delete(SavedAccountScheme.TABLE_NAME, null, null);
	}

	public SavedAccountEntity getAll() {
		Log.d("LOG", "step 3.3.1");
		String sql = "SELECT * from " + SavedAccountScheme.TABLE_NAME;
		Cursor mcursor = db.rawQuery(sql, null);
		if (mcursor.getCount() == 0)
			return null;
		mcursor.moveToFirst();
		SavedAccountEntity savedAccountEntity = new SavedAccountEntity(
				mcursor.getInt(0), mcursor.getString(1), mcursor.getString(2));
		mcursor.close();
		return savedAccountEntity;
	}

}
